package com.rt.easyjava.base.modules.timerjob.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.base.modules.timerjob.bean.TmptTimerBiz;
import com.rt.easyjava.base.modules.timerjob.dto.TmptTimerBizDto;
import com.rt.easyjava.base.modules.timerjob.service.intf.TmptTimerBizService;
import com.rt.easyjava.common.base.CommonDto;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.exception.ControllerException;
import com.rt.easyjava.common.utils.others.DateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Method;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2020-06-01
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson)
 */
public class TmptTimerController extends ControllerException {
    private final Logger logger = LoggerFactory.getLogger(TmptTimerController.class);

    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected TmptTimerBizService tmptTimerBizService;


    protected Result page(TmptTimerBizDto tmptTimerBizDto){
        Result<TmptTimerBizDto> result = new Result<>();

        Page<TmptTimerBiz> page = new Page<>(tmptTimerBizDto.getPage(), tmptTimerBizDto.getLimit());

        TmptTimerBiz tmptTimerBiz = mapper.map(tmptTimerBizDto, TmptTimerBiz.class);
        if(tmptTimerBiz.getDeleteflag()==null){
            tmptTimerBiz.setDeleteflag(0);
        }

        QueryWrapper<TmptTimerBiz> queryWrapper=new QueryWrapper<>(tmptTimerBiz);
        //特殊查询条件
        queryWrapper.gt(tmptTimerBizDto.getCreatedate_Start()!=null,"createdate",tmptTimerBizDto.getCreatedate_Start());
        queryWrapper.lt(tmptTimerBizDto.getCreatedate_End()!=null,"createdate",tmptTimerBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptTimerBizDto.getCondition(), tmptTimerBizDto.isAsc(), tmptTimerBizDto.getColumn());

        return tmptTimerBizService.page(page, queryWrapper);
    }

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    protected Result tree (Long id) {
        Result<TmptTimerBizDto> result = new Result<>();
        // 树递归
        TmptTimerBiz tmptTimerBiz = tmptTimerBizService.getById(id);
        TmptTimerBizDto tmptTimerBizDto = mapper.map(tmptTimerBiz, TmptTimerBizDto.class);
        treeReduce(tmptTimerBizDto);
        result.setObj(tmptTimerBizDto);

        return result.success();
    }

    private void treeReduce(TmptTimerBizDto tmptTimerBizDto){
    }

    protected Result myPage(TmptTimerBizDto tmptTimerBizDto){
        Result<TmptTimerBizDto> result = new Result<>();

        Page<TmptTimerBiz> page = new Page<>(tmptTimerBizDto.getPage(), tmptTimerBizDto.getLimit());

        TmptTimerBiz tmptTimerBiz = mapper.map(tmptTimerBizDto, TmptTimerBiz.class);
        if(tmptTimerBiz.getDeleteflag()==null){
            tmptTimerBiz.setDeleteflag(0);
        }

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptTimerBiz> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptTimerBiz , "createdate");
        //特殊查询条件
        queryWrapper.gt(tmptTimerBizDto.getCreatedate_Start()!=null,"t1.createdate",tmptTimerBizDto.getCreatedate_Start());
        queryWrapper.lt(tmptTimerBizDto.getCreatedate_End()!=null,"t1.createdate",tmptTimerBizDto.getCreatedate_End());
        // queryWrapper.orderBy(tmptTimerBizDto.getCondition(), tmptTimerBizDto.isAsc(), "t1."+tmptTimerBizDto.getColumn());
        for (CommonDto sortDto: tmptTimerBizDto.getSortList()){
          queryWrapper.orderBy(true, sortDto.getAsc(), sortDto.getColumn());
        }
        result = tmptTimerBizService.myPage(page, queryWrapper);

        return result;
    }

    protected Result selectBy(TmptTimerBizDto tmptTimerBizDto){
        Result<TmptTimerBizDto> result = new Result<>();

        TmptTimerBiz tmptTimerBiz = mapper.map(tmptTimerBizDto, TmptTimerBiz.class);
        if(tmptTimerBiz.getDeleteflag()==null){
            tmptTimerBiz.setDeleteflag(0);
        }

        QueryWrapper<TmptTimerBiz> queryWrapper=new QueryWrapper<>(tmptTimerBiz);
        //特殊查询条件
        queryWrapper.gt(tmptTimerBizDto.getCreatedate_Start()!=null,"createdate",tmptTimerBizDto.getCreatedate_Start());
        queryWrapper.lt(tmptTimerBizDto.getCreatedate_End()!=null,"createdate",tmptTimerBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptTimerBizDto.getCondition(), tmptTimerBizDto.isAsc(), tmptTimerBizDto.getColumn());

        result = tmptTimerBizService.selectBy(queryWrapper);

        return result;
    }

    protected Result selectById(Long id){
        //调用服务
        return tmptTimerBizService.selectById(id);
    }

    protected Result select4column(String columnName){
        try {
            Object o = SpringContextHolder.getBean("tmptTimerBizService");
            Method method = o.getClass().getMethod("mySelect4"+columnName);
            return Result.builder(method.invoke(o)).success();
        }catch (Exception e){
            throw new RuntimeException("方法名不存在");
        }
    }

    protected Result create(@RequestBody TmptTimerBizDto tmptTimerBizDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptTimerBizDto.setCreatemanId(Long.parseLong(_userId));
        tmptTimerBizDto.setCreateman(_userName);
        tmptTimerBizDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptTimerBizDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptTimerBizService.create(tmptTimerBizDto);
    }

    protected Result update(@RequestBody TmptTimerBizDto tmptTimerBizDto,
                        String _userId, String _userName){
        //参数
        tmptTimerBizDto.setModifymanId(Long.parseLong(_userId));
        tmptTimerBizDto.setModifyman(_userName);
        tmptTimerBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptTimerBizDto.setCreatedate(null);
        tmptTimerBizDto.setCreateman(null);
        tmptTimerBizDto.setCreatemanId(null);
        tmptTimerBizDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptTimerBizDto.getId()==null?"0":tmptTimerBizDto.getId().longValue());

        //调用服务
        return tmptTimerBizService.update(tmptTimerBizDto, updateWrapper);
    }

    protected Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptTimerBizDto tmptTimerBizDto = new TmptTimerBizDto();
        tmptTimerBizDto.setModifymanId(Long.parseLong(_userId));
        tmptTimerBizDto.setModifyman(_userName);
        tmptTimerBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptTimerBizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptTimerBizService.update(tmptTimerBizDto, updateWrapper);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptTimerBizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
